package com.dirox.jira.notes.utils;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.sal.api.ApplicationProperties;
import com.atlassian.sal.api.user.UserManager;
import static com.google.common.base.Preconditions.checkNotNull;
import org.apache.commons.lang.StringEscapeUtils;


/**
 *
 * This class is used for posting an activity
 *
 * @author thuan
 *
 */
public class JiraHelper {
	private ApplicationProperties applicationProperties;
	private UserManager salUserManager;

	public void setApplicationProperties(ApplicationProperties applicationProperties) {
		this.applicationProperties = checkNotNull(applicationProperties);
	}

	public void setSalUserManager(UserManager salUserManager) {
		this.salUserManager = checkNotNull(salUserManager);
	}

	public String getIssueLink(String issueKey) {
		return new StringBuffer(applicationProperties.getBaseUrl()).append(
			"/browse/")
		                                                           .append(
			issueKey)
		                                                           .toString();
	}

	public String getIssueLinkHtml(String issueKey) {
		return new StringBuffer("<a href='").append(
			applicationProperties.getBaseUrl())
		                                    .append("/browse/")
		                                    .append(issueKey)
		                                    .append("'>")
		                                    .append(issueKey)
		                                    .append("</a>")
		                                    .toString();
	}

	public String getProfileLink(String userName) {
		return new StringBuffer(applicationProperties.getBaseUrl()).append(
			salUserManager.getUserProfile(userName).getProfilePageUri().toString())
		                                                           .toString();
	}

	public String getProfileLinkHtml(User user) {
		return new StringBuffer("<a href='").append(
			applicationProperties.getBaseUrl())
		                                    .append(
			salUserManager.getUserProfile(user.getName()).getProfilePageUri().toString())
		                                    .append("'>")
		                                    .append(user.getDisplayName())
		                                    .append("</a>")
		                                    .toString();
	}

	public String htmlEncode(String str) {
		return StringEscapeUtils.escapeHtml(str);
	}
}
